package com.example.mydialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by Alex-Zhang on 2017/11/20.
 * 自定义Dialog
 */

public class MyDialog extends Dialog {

    public MyDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {
        private Context context;
        private String title, message, positiveContent, negativeContent;
        private OnClickListener positiveClickerListener;
        private OnClickListener negativeClickerListener;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setPositiveButton(int positiveButtonContentId, OnClickListener clickListener) {
            this.positiveContent = (String) context.getText(positiveButtonContentId);
            this.positiveClickerListener = clickListener;
            return this;
        }

        public Builder setPositiveContent(String positiveContent, OnClickListener clickListener) {
            this.positiveContent = positiveContent;
            this.positiveClickerListener = clickListener;
            return this;
        }


        public Builder setNegativeButton(int negativeButtonContentId, OnClickListener clickListener) {
            this.negativeContent = (String) context.getText(negativeButtonContentId);
            this.negativeClickerListener = clickListener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonContent, OnClickListener clickListener) {
            this.negativeContent = negativeButtonContent;
            this.negativeClickerListener = clickListener;
            return this;
        }


        public MyDialog create() {
            final MyDialog dialog = new MyDialog(context, R.style.Theme_AppCompat_Dialog);
            View view = LayoutInflater.from(context).inflate(0, null, false);
            dialog.addContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            if (positiveContent != null) {
                ((Button) view.findViewById(0)).setText(positiveContent);
                if (positiveClickerListener != null) {
                    ((Button) view.findViewById(0)).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            positiveClickerListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                        }
                    });
                } else {
                    view.findViewById(0).setVisibility(View.GONE);
                }
            }

            if (negativeContent != null) {
                ((Button) view.findViewById(0)).setText(negativeContent);
                if (negativeClickerListener != null) {
                    ((Button) view.findViewById(0)).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            negativeClickerListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                        }
                    });
                }
            } else {
                view.findViewById(0).setVisibility(View.GONE);
            }
            if (title != null) {
                ((TextView) view.findViewById(R.id.title)).setText(title);
            }
            if (message != null) {
                ((TextView) view.findViewById(0)).setText(message);
            }
            return dialog;
        }
    }
}
